package vsm.vsm_server.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vsm.vsm_server.pojo.Result.Result;
import vsm.vsm_server.pojo.Result.ResultEnum;
import vsm.vsm_server.pojo.User.UserInfo;
import vsm.vsm_server.pojo.User.Login.Login;
import vsm.vsm_server.pojo.User.Login.Register;
import vsm.vsm_server.pojo.User.User;
import vsm.vsm_server.service.RelationService;
import vsm.vsm_server.service.UserService;
import vsm.vsm_server.service.VodService;

import java.math.BigInteger;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/users")
@CrossOrigin
public class UserController {

    public static UserController uci;
    public UserController(){
        uci = this;
    }
    @Autowired
    private UserService userService;
    @Autowired
    private RelationService relationService;
    @Autowired
    private VodService vodService;
    /*
    根据指定id删除用户
     */
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable BigInteger id){
        log.info("根据指定id：{}删除用户",id);
        boolean state = userService.delete(id);
        if(!state){
            return Result.error(ResultEnum.USER_NOT_EXIST);
        }
        return Result.success();
    }
    /*
    修改用户基本信息
     */
    @PutMapping("/update")
    public Result<String> update(@RequestBody UserInfo userInfo){
        log.info("修改用户基本信息:{}", userInfo);

        if(!userService.update(userInfo)){
            return Result.error(ResultEnum.DATA_IS_WRONG);
        }
        return  Result.success();
    }
/*    *//*
    根据指定参数修改用户
     *//*
    @PutMapping
    public Result<String> update(@RequestBody User newuser){
        log.info("根据所给参数生成用户数据:{}",newuser);

        //将密码加密后存储？

        //
        if(userService.update(newuser))
            return Result.success();
        else
            return Result.error("修改失败");
    }*/
    /*
    注册 用户
     */
    @PostMapping("/register")
    public Result<BigInteger> register(@RequestBody Register register){
        log.info("新用户为{}",register);
        BigInteger userid =  userService.create(register);
        return Result.success(userid);
    }
    /*
    验证密码
     */
    @PostMapping("/login")
    public Result<String> verifyPwd(@RequestBody Login login){
        log.info("登录用户为{}",login);

      try {
            User user = userService.getById(login.getUserId());
            boolean matchResult = Objects.equals(login.getUserPwd(), user.getUserPwd());
            if(matchResult){


                return Result.success();
            }else{
                return Result.error(ResultEnum.USER_LOGIN_ERROR);
            }

        } catch (Exception e) {
            return Result.error(ResultEnum.USER_NOT_EXIST);
        }

    }

    /*
    根据id查找用户
     */
    @GetMapping("/{id}")
    public Result<User> getUser(@PathVariable BigInteger id){
        log.info("根据指定id：{}查询用户",id);
        userService.verifyFans(id);
        User theuser = userService.getById(id);
        if(theuser != null){
            theuser.setUserPwd("");
            if(theuser.getAvatar()!=null){
                /*theuser.setAvatar(vodService.getPicture(theuser.getAvatar()));*/
            }
            return Result.success(theuser);
        }
        else
            return Result.error_special(ResultEnum.USER_NOT_EXIST);

    }

}
